<?php
namespace app\api\controller\vppz;


use think\Cache;


use \app\admin\model\vppz\Specialist as SpecialistModel;
use \addons\vppz\library\Vpower;
use function fast\e;

class Special extends App
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->SpecialistModel = new SpecialistModel;
    }


    /**
     * 科室列表
     */
    public function keshi(){

        $keshi = $this->SpecialistModel->whereNull('deletetime')
            ->order('id','DESC')
            ->group('keshiname')
            ->column('keshiname');
		$this->success('',array(
			'now'=>time(),
			'keshi'=>$keshi
		));
    }

    /**
     * 医院列表
     */
    public function hospital(){
        $hospital = $this->SpecialistModel->whereNull('deletetime')
            ->order('id','DESC')
            ->group('hospitalname')
            ->column('hospitalname');
		$this->success('',array(
			'now'=>time(),
			'hospital'=>$hospital
		));
    }
    public function listsindex(){

        $specialsmodel = $this->SpecialistModel->whereNull('deletetime');
        $keyword = input('keyword','');
        $zhicheng = input('zhicheng','');
        $hospital = input('hospital','');
        $special = input('special','');
        $page_no = input('page_no',1);
        $page_size = input('page_size',10);
        if (empty($keyword) === FALSE){
            $specialsmodel = $specialsmodel->whereLike('name|hospitalname|keshiname|disease_fanwei|field','%'.$keyword.'%');
        }
        if(empty($zhicheng) === FALSE){
            $specialsmodel = $specialsmodel->where('zhicheng','=',$zhicheng);
        }
        if(empty($hospital) === FALSE){
            $specialsmodel = $specialsmodel->where('hospitalname','=',$hospital);
        }
        if(empty($special) === FALSE){
            $specialsmodel = $specialsmodel->where('keshiname','=',$special);
        }
        $model = $specialsmodel;
        $resspecials = $specialsmodel->order('weigh','DESC')
            ->limit(($page_no-1)*$page_size,$page_size)
            ->select();

        $model = $this->SpecialistModel->whereNull('deletetime');
        if(empty($keyword) === FALSE){
            $model = $model->whereLike('name|hospitalname|keshiname|disease_fanwei|field','%'.$keyword.'%');
        }
        if(empty($zhicheng) === FALSE){
            $model = $model->where('zhicheng','=',$zhicheng);
        }
        if(empty($hospital) === FALSE){
            $model = $model->where('hospitalname','=',$hospital);
        }
        if(empty($special) === FALSE){
            $model = $model->where('keshiname','=',$special);
        }
        $total = $model->count('id');
        if(empty($resspecials) === FALSE){
            foreach ($resspecials as $bk=>$special){
                $resspecials[$bk]['createtime'] = date('Y-m-d H:i:s',$special['createtime']);
                $resspecials[$bk]['disease_fanwei'] = explode(',',$special['disease_fanwei']);
            }
        }else{
            $resspecials = [];
        }
		$this->success('',array(
			'now'=>time(),
			'special'=>$resspecials,
            'total'=>$total
		));
    }
/**
     *专家详情
     */
    public function detail(){

		$id = input('id');

		$special = $this->SpecialistModel->find($id);
		if(empty($special)){
			$this->error('不存在或暂停提供');
		}
        $special['createtime'] = date('Y-m-d H:i:s',$special['createtime']);
        $special['disease_fanwei'] = explode(',',$special['disease_fanwei']);
		$this->success('',array(
			'now'=>time(),
			'special'=>$special
		));
    }
}
